import Vue from 'vue'
import VueRouter from 'vue-router'
import {Message} from "element-ui";
import {getToken, removeToken} from "@/store/token";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: "home",
        component: () => import('../views/home.vue'),
        children: [
            {
                path: '/topBook',
                name: 'topBook',
                component: () => import('../views/topBooks.vue')
            }, {
                path: '/type',
                name: 'typeBook',
                component: () => import('../views/typeBook.vue'),
            }, {
                path: '/search',
                name: 'search',
                component: () => import( '../views/searchBook.vue')
            }, {
                path: '/detail',
                name: 'detail',
                component: () => import( '../views/detail.vue')
            },
        ]
    }, {
        path: '/more',
        name: 'more',
        component: () => import( '../views/MoreBook.vue')
    }, {
        path: '/resetPassword',
        name: 'resetPassword',
        component: () => import( '../views/restPassword.vue')
    }, {
        path: '/login',
        name: 'login',
        component: () => import( '../views/Login.vue')
    }, {
        path: '/register',
        name: 'register',
        component: () => import( '../views/Register.vue')
    }, {
        path: '/pay',
        name: 'pay',
        component: () => import( '../views/PayPage.vue'),
        meta: {
            needLogin: true //需要加校检判断的路由
        },
    }, {
        path: '/userDetail',
        name: 'userDetail',
        component: () => import( '../views/user/userDetail.vue'),
        redirect: "/myInfo",
        children: [
            {
                path: '/myInfo',
                name: 'myInfo',
                component: () => import('../views/user/myInfo.vue'),
                meta: {
                    needLogin: true //需要加校检判断的路由
                },
            }, {
                path: '/userSafety',
                name: 'userSafety',
                component: () => import('../views/user/userSafety.vue'),
                meta: {
                    needLogin: true //需要加校检判断的路由
                },
            }, {
                path: '/myAddress',
                name: 'myAddress',
                component: () => import('../views/user/myAddress.vue'),
                meta: {
                    needLogin: true //需要加校检判断的路由
                },
            }, {
                path: '/myOrder',
                name: 'myOrder',
                component: () => import('../views/user/myOrder.vue'),
                meta: {
                    needLogin: true //需要加校检判断的路由
                },
            }, {
                path: '/myAfterSale',
                name: 'myAfterSale',
                component: () => import('../views/user/myAfterSale.vue'),
                meta: {
                    needLogin: true //需要加校检判断的路由
                },
            }, {
                path: '/myCart',
                name: 'myCart',
                component: () => import('../views/user/myCart.vue'),
                meta: {
                    needLogin: true //需要加校检判断的路由
                },
            },
        ]
    },
    {
        path: '/admin',
        name: 'admin',
        component: () => import('../views/admin/Index.vue'),
        redirect: "/aHome",
        children: [
            {
                path: '/aHome',
                name: 'aHome',
                component: () => import('../views/admin/Home.vue'),
                meta: {
                    needLogin: true, //需要加校检判断的路由
                    role: "admin"
                },
            }, {
                path: '/qk',
                name: 'qk',
                component: () => import('../views/admin/qk.vue'),
                meta: {
                    needLogin: true, //需要加校检判断的路由
                    role: "admin"
                },
            }, {
                path: '/qkType',
                name: 'qkType',
                component: () => import('../views/admin/type.vue'),
                meta: {
                    needLogin: true, //需要加校检判断的路由
                    role: "admin"
                },
            }, {
                path: '/order',
                name: 'order',
                component: () => import('../views/admin/order.vue'),
                meta: {
                    needLogin: true, //需要加校检判断的路由
                    role: "admin"
                },
            }, {
                path: '/afterSale',
                name: 'afterSale',
                component: () => import('../views/admin/afterSale.vue'),
                meta: {
                    needLogin: true, //需要加校检判断的路由
                    role: "admin"
                },
            }, {
                path: '/user',
                name: 'user',
                component: () => import('../views/admin/user.vue'),
                meta: {
                    needLogin: true, //需要加校检判断的路由
                    role: "admin"
                },
            },
        ]
    },
]

const router = new VueRouter({
    mode: 'history',
    routes
})

//验证是否已经登录
router.beforeEach((to, from, next) => {
    if (to.meta.needLogin === true) {
        let token = localStorage.getItem('Authorization');
        // 获取存储localStorage的token
        //let token = window.localStorage.getItem('Authorization')
        // 获取存储token的开始时间
        const tokenStartTime = window.localStorage.getItem('tokenStartTime')
        // 后台给出的token有效时间，一个星期 单位 是秒
        // 我们自己定义1天过期，让用户重新登录一下， 用户总不可能在一个页面挂机一天吧
        const timeOver = 24 * 3600 * 1000
        // 当前时间
        let date = new Date().getTime()
        //// 如果大于说明是token过期了
        if (date - tokenStartTime > timeOver) {
            removeToken('Authorization');
            removeToken("user")
            removeToken("role")
            removeToken("tokenStartTime")
            token = null
        }
        // 如果token过期了
        if (!token) {
            if (to.path === '/login') return next()
            // 注意要import element的Message组件
            Message.error("登录状态过期,或者未登录，请登录")
            next('/')
        } else {
            //验证是否是管理员
            if (getToken("role") === to.meta.role) {
                next()
            } else if (to.meta.role === undefined) {
                next();
            } else {
                next('/')
            }
        }
    } else {
        next();
    }


});
///** 解决跳转重复路由报错问题 */
//const routerPush = router.push;
//router.push = path => {
//    // 判断下当前路由是否就是要跳转的路由
//    if (router.currentRoute.fullPath.includes(path)) return;
//    return routerPush.call(router, path);
//}
// 避免跳转当前页面报错
const changePush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return changePush.call(this, location).catch((err) => err);
};

export default router
